package com.sg.java.apps;

import cn.hutool.core.util.ObjectUtil;


import com.sg.java.util.CsvUtils;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;


import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Test {

    //用户关联关系
    private static HashMap<String, String> yhMap = new HashMap<>();

    //用户和配变关系
    private static HashMap<String, String> yhPidMap = new HashMap<>();

    //配变测点关系
    private static HashMap<String, String> pbGxMap = new HashMap<>();

    public static void main(String[] args) throws Exception {

        String yhgx = "D:\\code\\shanggu\\chongqing\\导出\\20220817pbgx111";

        //读取关系配置
        String[] headers = new String[]{"meter_id", "tg_id", "volt_code", "tran_name", "org_no", "sfnw", "equip_id", "plate_cap", "xh", "tyrq"};
        String filePath = System.getProperty("user.dir") + File.separator + "conf" + File.separator;
        //填充用户到测点的关系map

        List<String> list = new ArrayList<>();
        pbGxMap = CsvToPbCd(yhgx, headers);


//        write_csv(pbGxMap);




        System.out.println(list);

    }


    public static HashMap<String, String> CsvToPbCd(String filePath, String[] headers) throws Exception {

//        String file = findFile(filePath, fileName);
        HashMap<String, String> hashMap = new HashMap<>();
        //创建CSVFormat
        CSVFormat formator = CSVFormat.DEFAULT.withHeader(headers);

        FileReader fileReader = null;
        CSVParser parser = null;
        int num = 0;
        try {
            fileReader = new FileReader(filePath);
            parser = new CSVParser(fileReader, formator);
            for (CSVRecord next : parser) {

                if (ObjectUtil.isNull(next)) {
                    continue;
                }

//         //全部的配变都先按找220V来算
                String str = next.get(0) + "!@@" + next.get(1) + "!@@" + next.get(2) + "!@@" + next.get(3) + "!@@" + next.get(4) + "!@@" + next.get(5) + "!@@" + next.get(6) + "!@@" + next.get(7) + "!@@" + next.get(8) + "!@@" + next.get(9);
                hashMap.put(next.get(1), str);


//                String ctKey = "RDT@" + month + "@" + next.get(4) + "@" + next.get(1);
//                jp.sadd(ctKey, next.get(1));
////                jedis.sadd(ctKey, next.get(1));
//                num++;
//                if (num > 10000) {
//                    jp.sync();
//                    num = 0;
//                }
            }
//            jp.sync();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                parser.close();
                fileReader.close();
//                jp.close();
//                jedis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return hashMap;
    }

    public static String findFile(String filePath, String zpbyhName) throws Exception {
        File file = new File(filePath);
        String[] names = file.list();
        for (String name : names) {
            boolean a = name.startsWith(zpbyhName);
            if (a) {
                return filePath + name;
            }
        }

        throw new Exception("文件名有误");
    }


    public static void write_csv(HashMap<String, String> data) throws IOException {

        // 创建输出流
        Appendable out = new PrintWriter("dept2.csv");
        //CSVFormat csvFormat = CSVFormat.newFormat('\t').withHeader(header).withSkipHeaderRecord();
        // 注意 使用 DEFAULT 创建的CSVFormat对象生成记录可以自动换行,
        // 如果使用上面的 newFormat 的csvformat 不会自动换行
        CSVPrinter csvPrinter = CSVFormat.DEFAULT.print(out);
        /* CSVPrinter csvPrinter = new CSVPrinter(out, csvFormat);*/

        // 创建 一些记录信息
        List<String> records = new ArrayList<>();
        // 部门编号,
//        records.add("RSA001_20190121" + "\t" + "产品部" + "\t" + "RSA001");
//        records.add("RSA002_20180623" + "\t" + "设计部" + "\t" + "RSA002");
//        records.add("RSA003_20150422" + "\t" + "技术部" + "\t" + "RSA003");
//        records.add("RSA004_20160903" + "\t" + "运营部" + "\t" + "RSA004");
//        records.add("RSA005_20120608" + "\t" + "财务部" + "\t" + "RSA005");


        data.forEach((k, v) ->
        {
            String[] split = v.split("!@@");
            records.add(split[0] + "\t" + split[1]);

        });

        // 将数据写入到 文件中
        for (String record : records) {
            csvPrinter.printRecord(record);
        }

        // 刷写缓冲区
        csvPrinter.flush();
        csvPrinter.close();
    }

    /**
     * 写入csv文件
     *
     * @param headers  列头
     * @param data     数据内容
     * @param filePath 创建的csv文件路径
     */
    public static void writeCsv(String[] headers, HashMap<String, String> data, String filePath) {
        try {
            //初始化csvformat
            //withRecordSeparator 行分隔符,默认就是 \n
            //withDelimiter 字段分隔符
            CSVFormat formator = CSVFormat.DEFAULT
                    .withRecordSeparator("\n")
                    .withDelimiter(',')
                    // 设置分隔符
                    .withDelimiter('\t');

            //创建FileWriter对象
            //方式1
            //FileWriter fileWriter=new FileWriter(filePath);

            //方式2
            FileOutputStream fos = new FileOutputStream(filePath);
            OutputStreamWriter fileWriter = new OutputStreamWriter(fos, "UTF-8");

            //创建CSVPrinter对象
            CSVPrinter printer = new CSVPrinter(fileWriter, formator);

            //写入列头数据
            printer.printRecord(headers);

            if (null != data) {



                printer.printRecord();

//
//                for (AdsGridGddyCCons datum : data) {
//                    printer.printRecord(datum.getConsId() + "," + datum.getConsSortCode() + "," + datum.getVoltCode() + "," + datum.getOrgNo());
//
//                }

            }
            //记得添加，不然没有输出
            printer.flush();
            printer.close();
            System.out.println("CSV文件创建成功,文件路径:" + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}